package work.xiaohong.mybatisplus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import work.xiaohong.utils.StatusCode;

import java.util.*;

/**
 * @author xiaohong
 * @version 1.0
 * @date 2021/10/20 0020 12:32
 * @description Swagger配置
 */
@Configuration
@EnableSwagger2
@EnableOpenApi
public class SwaggerConfig2 implements WebMvcConfigurer {

    //配置了Swagger的Docket的bean实例
    //enable是否启动swagger，如果为False则Swagger不能在浏览器访问
    @Bean
    public Docket docket() {

        // 设置全局响应状态码
        List<Response> responseList = new ArrayList<>();
        for (StatusCode statusCode : StatusCode.values()) {
            String description = statusCode.getDescription();
            String code = statusCode.getCode().toString();
            responseList.add(new ResponseBuilder().code(code).description(description).build());
        }
        // 支持的通讯协议集合
        Set<String> set = new HashSet<>();
        set.add("https");
        set.add("http");

        return new Docket(
                DocumentationType.SWAGGER_2)
                .enable(true)//定义是否开启swagger，false为关闭，可以通过变量控制
                .apiInfo(apiInfo())//将api的元信息设置为包含在json ResourceListing响应中。
                .select()
                .apis(RequestHandlerSelectors.basePackage("work.xiaohong"))
                //paths()过滤什么路径
                .paths(PathSelectors.any())
                .build()
                .protocols(set)
                //下面这个设置就是在接口的path前加上api
                .pathMapping("/")
                // 全局响应配置
                .globalResponses(HttpMethod.GET,responseList)
                .globalResponses(HttpMethod.DELETE,responseList)
                .globalResponses(HttpMethod.POST,responseList)
                .globalResponses(HttpMethod.PUT,responseList)
                .globalResponses(HttpMethod.PATCH,responseList)
                .globalResponses(HttpMethod.HEAD,responseList)
                .globalResponses(HttpMethod.OPTIONS,responseList)
                .globalResponses(HttpMethod.TRACE,responseList)
                .securitySchemes(securitySchemes())// 授权信息设置，必要的header token等认证信息
                .securityContexts(securityContexts());// 授权信息全局应用
    }


    //作者信息
    Contact contact = new Contact("小鸿","https://www.xiaohong.work","1990063814@qq.com");

    //配置Swagger 信息 = ApiInfo
    private ApiInfo apiInfo()
    {
        return new ApiInfo("用户微服务",
                "用户微服务用户Api文档",
                "1.0.0",
                "https://www.xiaohong.work",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());
    }

    /**
     * 设置授权信息
     */
    private List<SecurityScheme> securitySchemes()
    {
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("xiaohong-token","身份令牌" ,"Header" );
        result.add(apiKey);
        return  Collections.singletonList(apiKey);
    }

    /**
     * 授权信息全局应用
     */
    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        //这里第一个参数要对应上面创建的ApiKey的name值
                        .securityReferences(Collections.singletonList(new SecurityReference("xiaohong-token", new AuthorizationScope[]{new AuthorizationScope("global", "身份令牌")})))
                        .build()
        );
    }

}
